package com.bm.beautifulmouse.effect;

import com.bm.beautifulmouse.model.EffectConfig;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
import java.util.Random;

public class SnowEffect implements Effect {
    private final Random random = new Random();

    @Override
    public void generate(Pane container, double x, double y, EffectConfig config) {
        for (int i = 0; i < config.getParticleCount(); i++) {
            Circle snowflake = new Circle(2 * config.getSize());
            // 设置颜色
            snowflake.setFill(config.getColor());
            
            snowflake.setCenterX(x);
            snowflake.setCenterY(y);

            double angle = random.nextDouble() * 360;
            double distance = random.nextDouble() * 50 * config.getSize() * config.getRange();
            double duration = 2.0 / config.getSpeed();

            Timeline timeline = new Timeline(
                    new KeyFrame(Duration.ZERO,
                            new KeyValue(snowflake.centerXProperty(), x),
                            new KeyValue(snowflake.centerYProperty(), y),
                            new KeyValue(snowflake.opacityProperty(), 1.0)
                    ),
                    new KeyFrame(Duration.seconds(duration),
                            new KeyValue(snowflake.centerXProperty(), x + Math.cos(angle) * distance),
                            new KeyValue(snowflake.centerYProperty(), y + Math.sin(angle) * distance),
                            new KeyValue(snowflake.opacityProperty(), 0.0)
                    )
            );

            timeline.setOnFinished(e -> container.getChildren().remove(snowflake));
            container.getChildren().add(snowflake);
            timeline.play();
        }
    }
}